<?php
namespace App\Service\Front;

use Illuminate\Support\Facades\DB;

class PayService extends BaseService
{
    //支付

    //通过token获取openid
    public function userByToken($token)
    {
        $uinfo = '';
        $resOne = (array) $this->dbConn($this->dbUser)
            ->where('token', $token)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($resOne)) {
            return $uinfo;
        }
        return $resOne;
    }

    /**
     * 获取订单价格
     * @param int $type 1原创需求订单
     */
    public function orderOne($type, $orderno)
    {
        $arys = [];
        if ($type == 1) {
            $orderOne = (array) $this->dbConn($this->dbCxOrder)
                ->where('del', 0)
                ->where('orderno', $orderno)
                //->where('status', 1)
                ->first();
            if (empty($orderOne)) {
                return $this->toback(-2, '没有订单');
            }
            if ($orderOne['status'] == 2) {
                return $this->toback(-2, '已落选');
            }
            if ($orderOne['status'] >= 4) {
                return $this->toback(-2, '已支付');
            }
            $payOne = (array) $this->dbConn($this->dbPay)
                ->where('from_type', $type)
                ->where('out_trade_no', $orderno)
                ->where('status', 2)
                ->first();
            if (!empty($payOne)) {
                return $this->toback(-2, '已支付');
            }
            $xqOne = (array) $this->dbConn($this->dbCaseXq)
                ->where('id', $orderOne['case_id'])
                ->first();
            if (empty($xqOne)) {
                return $this->toback(-2, '需求错误');
            }
            $arys['title'] = $xqOne['title'];
            $arys['orderno'] = $orderno;
            //$arys['price'] = toNumber($res['price']);
            $arys['price'] = $orderOne['price'];
        } else {
            return $this->toback(-1, 'error');
        }
        return $this->toback(0, '', $arys);
    }

    //微信添加到pay表
    public function wxPayAdd($pas)
    {
        $pas['pay_way'] = 1;
        $pas['addtime'] = toDate();
        return $this->dbConn($this->dbPay)->insertGetId($pas);
    }

    //微信更新pay表
    public function wxPayModify($id, $pas)
    {
        $pas['uptime'] = toDate();
        return $this->dbConn($this->dbPay)->where('id', $id)->update($pas);
    }

    //微信通知
    public function wxNotice($ndata)
    {
        $wxjson = [
            'pay_way' => 1,
            'otherjson' => json_encode($ndata, JSON_UNESCAPED_UNICODE),
            'addtime' => toDate(),
        ];
        return $this->dbConn($this->dbPayNotice)->insertGetId($wxjson);
    }

    //微信成功更新
    public function wxNoticeModify($id, $out_trade_no)
    {
        $payOne = (array) $this->dbConn($this->dbPay)->where('out_trade_no', $out_trade_no)->first();
        if (empty($payOne)) {
            return $this->toback(-2, '没有支付记录');
        }
        $type = $payOne['from_type'];
        if ($type == 1) {
            $cxOrderOne = (array) $this->dbConn($this->dbCxOrder)
                ->where('orderno', $out_trade_no)
                ->first();
        }
        DB::beginTransaction(); //开启事务
        try {
            //更新通知
            $this->dbConn($this->dbPayNotice)->where('id', $id)
                ->update(['out_trade_no' => $out_trade_no]);
            //更新支付状态
            $this->dbConn($this->dbPay)->where('out_trade_no', $out_trade_no)
                ->update(['status' => 2, 'uptime' => toDate()]);
            //更新订单状态
            if (!empty($cxOrderOne) && $cxOrderOne['status'] == 3) {
                $this->dbConn($this->dbCxOrder)->where('id', $cxOrderOne['id'])
                    ->update(['status' => 4, 'paytime' => toDate()]);
            }
            DB::commit(); //提交事务
            return array('code'=>0, 'msg'=>'success');
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return array('code'=>-1, 'msg'=>'操作失败');
        }
    }

    //支付宝添加到pay表
    public function zfbPayAdd($pas)
    {
        $pas['pay_way'] = 2;
        $pas['addtime'] = toDate();
        return $this->dbConn($this->dbPay)->insertGetId($pas);
    }

    //支付宝更新pay表
    public function zfbPayModify($id, $pas)
    {
        $pas['uptime'] = toDate();
        return $this->dbConn($this->dbPay)->where('id', $id)->update($pas);
    }

    //支付宝通知
    public function zfbNotice($ndata)
    {
        $json = [
            'pay_way' => 2,
            'otherjson' => json_encode($ndata, JSON_UNESCAPED_UNICODE),
            'addtime' => toDate(),
        ];
        return $this->dbConn($this->dbPayNotice)->insertGetId($json);
    }

    //支付宝成功更新
    public function zfbNoticeModify($id, $out_trade_no)
    {
        $payOne = (array) $this->dbConn($this->dbPay)->where('out_trade_no', $out_trade_no)->first();
        if (empty($payOne)) {
            return $this->toback(-2, '没有支付记录');
        }
        $type = $payOne['from_type'];
        if ($type == 1) {
            $cxOrderOne = (array) $this->dbConn($this->dbCxOrder)
                ->where('orderno', $out_trade_no)
                ->first();
        }
        DB::beginTransaction(); //开启事务
        try {
            //更新通知
            $this->dbConn($this->dbPayNotice)->where('id', $id)
                ->update(['out_trade_no' => $out_trade_no]);
            //更新支付状态
            $this->dbConn($this->dbPay)->where('out_trade_no', $out_trade_no)
                ->update(['status' => 2, 'uptime' => toDate()]);
            //更新订单状态
            if (!empty($cxOrderOne) && $cxOrderOne['status'] == 3) {
                $this->dbConn($this->dbCxOrder)->where('id', $cxOrderOne['id'])
                    ->update(['status' => 4, 'paytime' => toDate()]);
            }
            DB::commit(); //提交事务
            return array('code'=>0, 'msg'=>'success');
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return array('code'=>-1, 'msg'=>'操作失败');
        }
    }

    //微信退款生成
    public function wxRefundAdd($orderno, $pas)
    {
        $orderOne = (array) $this->dbConn($this->dbPay)
            ->where('pay_way', 1)
            ->where('out_trade_no', $orderno)
            ->where('status', 2)
            ->first();
        if (empty($orderOne)) {
            return $this->toback(-2, '订单没有成功支付');
        }
        $payOne = (array) $this->dbConn($this->dbRefund)
            ->where('pay_way', 1)
            ->where('out_trade_no', $orderno)
            ->where('status', 2)
            ->first();
        if (!empty($payOne)) {
            return $this->toback(-2, '订单已退款成功');
        }
        $refundno = toOrderno();
        $rData = [
            'uid' => $payOne['uid'], 'pay_way' => 1,
            'out_trade_no' => $payOne['out_trade_no'],
            'money' => $payOne['money'], 'refundno' => $refundno,
            'other_req' => $pas, 'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbRefund)->insertGetId($rData);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback(0, '', $lastid);
    }
}